package com.pawelsekara.curves;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

public class MainMenuScene extends MenuScene {

	protected static final short MENU_PLAY = 0;
	protected static final short MENU_QUIT = MENU_PLAY + 1;
	protected static final short MENU_SOUND = MENU_QUIT + 1;

	private float ratio;
	private float tWidth;
	private float tHeight;

	public MainMenuScene(Camera pCamera) {
		super(pCamera);
		
		initMenuScene();
	}

	private void initMenuScene() {
		TextureRegion tTextureRegion = SceneManager.curvesgame.mainMenuMap
				.get("mainmenubg");
		ratio = CurvesGame.CAMERA_WIDTH / tTextureRegion.getWidth();
		tWidth = tTextureRegion.getWidth() * ratio;
		tHeight = tTextureRegion.getHeight() * ratio;

		Sprite background = new Sprite(
				0, CurvesGame.CAMERA_HEIGHT / 2 - tHeight / 2, tWidth, tHeight,
				tTextureRegion,
				SceneManager.curvesgame.getVertexBufferObjectManager()) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};
		attachChild(background);
		Text tText = new Text(0,
				0,
				SceneManager.curvesgame.fontsMap.get(Constants.FONT1_NAME),
				"CurvesGame", new TextOptions(HorizontalAlign.CENTER),
				SceneManager.curvesgame.getVertexBufferObjectManager());
		tText.setColor(new Color(0f, 0f, 0f));
		tText.setScale(1.25f, 1.25f);
		attachChild(tText);
		tText.setPosition(CurvesGame.CAMERA_WIDTH / 2 - tText.getWidth() / 2, CurvesGame.CAMERA_HEIGHT * 0.1f);
		
	//	new ScaleMenuItemDecorator(pMenuItem, pSelectedScale, pUnselectedScale)
		IMenuItem playMenuItem = new ScaleMenuItemDecorator(new TextMenuItem(
				MENU_PLAY,
				SceneManager.curvesgame.fontsMap.get(Constants.FONT1_NAME),
				"Play",
				SceneManager.curvesgame.getVertexBufferObjectManager()), 1.25f, 1f);
		addMenuItem(playMenuItem);
		playMenuItem.setPosition(CurvesGame.CAMERA_WIDTH / 2 - playMenuItem.getWidthScaled() / 2, 0);

		IMenuItem quitMenuItem = new ScaleMenuItemDecorator(new TextMenuItem(
				MENU_QUIT,
				SceneManager.curvesgame.fontsMap.get(Constants.FONT1_NAME),
				"Quit",
				SceneManager.curvesgame.getVertexBufferObjectManager()), 1.25f, 1f);
		addMenuItem(quitMenuItem);
		
		buildAnimations();
		
		setBackgroundEnabled(false);
		
		setOnMenuItemClickListener(new IOnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
					float pMenuItemLocalX, float pMenuItemLocalY) {
				
				switch(pMenuItem.getID()) {
				case MENU_PLAY:
					SceneManager.getManager().setScene(LevelSelectScene.getInstance());
					return true;
				case MENU_QUIT:
					System.gc();
					SceneManager.curvesgame.finish();
					return true;
				default:
					return false;
				}
			}
		});
	}
}
